package io.github.junxworks.qt.modules.stock.service.impl;

import java.lang.reflect.Field;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import io.github.junxworks.ep.core.orm.annotations.Column;
import io.github.junxworks.junx.core.util.ObjectUtils;
import io.github.junxworks.qt.modules.bi.vo.NvPair;
import io.github.junxworks.qt.modules.stock.dto.IndustryIndicatorCondition;
import io.github.junxworks.qt.modules.stock.dto.IndustryIndicatorDetailCondition;
import io.github.junxworks.qt.modules.stock.mapper.IndustryMapper;
import io.github.junxworks.qt.modules.stock.service.IndustryService;
import io.github.junxworks.qt.modules.stock.vo.IndustryIndicatorDetailVo;
import io.github.junxworks.qt.modules.stock.vo.IndustryStatVo;
import net.vidageek.mirror.list.dsl.MirrorList;

@Service
public class IndustryServiceImpl implements IndustryService {

	@Autowired
	private IndustryMapper industryMapper;

	@Override
	public List<IndustryIndicatorDetailVo> queryIndicatorsDetail(IndustryIndicatorDetailCondition condition) {
		return industryMapper.queryIndicatorsDetail(condition);
	}

	@Override
	public List<NvPair> statIndustryMoneyFlow(IndustryIndicatorDetailCondition condition) {
		return industryMapper.statIndustryMoneyFlow(condition);
	}

	@Override
	public List<NvPair> statIndustryMarketValue(IndustryIndicatorDetailCondition condition) {
		return industryMapper.statIndustryMarketValue(condition);
	}

	public static void generateFileds(Class<?> clazz) {
		MirrorList<Field> fs = ObjectUtils.mirror().on(clazz).reflectAll().fields();
		fs.forEach(f -> {
			Column c = f.getAnnotation(Column.class);
			String label = c.comment();
			String name = f.getName();
			int length = label.length() * 20;
			length = length < 120 ? 120 : length;
			length = length > 320 ? 320 : length;
			if (ObjectUtils.isNumeric(f.getType())) {
				System.out.println(",{field: '" + name + "', title: '" + label + "', align: 'right',width: " + length + ",templet:");
				System.out.println("function(d) { if(d." + name + "){ return numberFormat(d." + name + ",2);} return '-';}");
				System.out.println("}");
			} else {
				System.out.println(",{field: '" + name + "', title: '" + label + "', align: 'center',width: " + length + "}");
			}
		});
	}

	public static void main(String[] args) {
		generateFileds(IndustryIndicatorDetailVo.class);
	}

	@Override
	public List<IndustryStatVo> rankMarketValue(IndustryIndicatorCondition condition) {
		return industryMapper.rankMarketValue(condition);
	}

	@Override
	public List<IndustryStatVo> rankMoneyFlow(IndustryIndicatorCondition condition) {
		return industryMapper.rankMoneyFlow(condition);
	}

}
